package ro.k.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ro.k.web.beans.UserBean;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet(description = "invalidate session and redirect user to login", urlPatterns = { "/LogoutServlet" })
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LogoutServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession hs = request.getSession();
		UserBean ub = (UserBean) hs.getAttribute(KConstants.UserKey);
		if (ub != null) {
			hs.setAttribute(KConstants.UserKey, null);
			hs.removeAttribute(KConstants.UserKey);
			hs.invalidate();
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return;
		}
		hs.invalidate();
		response.sendRedirect(request.getContextPath() + "/index.jsp");
	}

}
